unit Form_Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, TeeProcs, TeEngine, Chart, AppInterface,
  ComCtrls;

type
  TfrmDiskSpace = class(TForm)
    txtDirToCount: TEdit;
    PaintBox: TPaintBox;
    StatusBar: TStatusBar;
    procedure FormCreate(Sender: TObject);
    procedure PaintBoxMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FormPaint(Sender: TObject);
    procedure FormResize(Sender: TObject);
  private
    SizeHint : THintWindow;
    function CountDirSize(Dir : String):Integer;
    procedure CountDir;
  public
    { Public declarations }
  end;

var
  frmDiskSpace: TfrmDiskSpace;
  Charts : Array of record
    Name : String;
    Size : Integer;
    Color : TColor;
  end;
  MaxSize, DirsCount : Integer;

implementation

{$R *.dfm}

{ TfrmDiskSpace }

procedure TfrmDiskSpace.CountDir;
var
  sr : TSearchRec;
begin
  MaxSize := 0;
  DirsCount := 0;
  SetLength(Charts, 0);
  if FindFirst(txtDirToCount.Text + '*.*', faDirectory, sr) = 0 then
  begin
    repeat
      if (sr.Name <> '.') and (sr.Name <> '..') and ((sr.Attr or faDirectory) <> 0) and (DirectoryExists(txtDirToCount.Text + sr.Name)) then
      begin
        StatusBar.Panels[0].Text := sr.Name;
        Application.ProcessMessages;
        SetLength(Charts, Length(Charts)+1);
        Charts[High(Charts)].Name := sr.Name;
        Charts[High(Charts)].Size := CountDirSize(txtDirToCount.Text + sr.Name + '\');
        Charts[High(Charts)].Color := RGB(Random(256),Random(256),Random(256));
        if Charts[High(Charts)].Size > MaxSize then
          MaxSize := Charts[High(Charts)].Size;
        Inc(DirsCount);
      end;
    until
      FindNext(sr) <> 0;
  end;
  StatusBar.SimpleText := '';
end;

function TfrmDiskSpace.CountDirSize(Dir: String): Integer;
var
  sr : TSearchRec;
begin
  Result := 0;
  if FindFirst(Dir + '*.*', faDirectory or faAnyFile, sr) = 0 then
  begin
    repeat
      if (sr.Name <> '.') and (sr.Name <> '..') then
      begin
        //StatusBar.SimpleText := sr.Name;
        if (sr.Attr and faDirectory) <> 0 then
          Result := CountDirSize(Dir + sr.Name + '/') else
            Result := Result + sr.Size;

        Application.ProcessMessages;
      end;
    until
      FindNext(sr) <> 0;
  end;
end;

procedure TfrmDiskSpace.FormCreate(Sender: TObject);
begin
  txtDirToCount.Text := AppFace.UI_GetActiveList^.Path;
end;

procedure TfrmDiskSpace.PaintBoxMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
var
  Index : Integer;
  p : TPoint;
begin
  if Length(Charts) <> 0 then
  begin
    Index := x div ( PaintBox.Width div Length(Charts));
    StatusBar.Panels[0].Text := Charts[Index].Name;// + '(' + AppFace.CO_GetSizeString(Charts[Index].Size) + ')';
    StatusBar.Panels[1].Text := AppFace.CO_GetSizeString(Charts[Index].Size);
{    if SizeHint = nil then
    begin
      SizeHint := THintWindow.Create(Self);
    end;
    GetCursorPos(p);
    SizeHint.Left := p.X;
    SizeHint.Top := p.Y;
    SizeHint.ActivateHint(SizeHint.CalcHintRect(100, AppFace.CO_GetSizeString(Charts[Index].Size), nil), AppFace.CO_GetSizeString(Charts[Index].Size));

 }
  end;
end;

procedure TfrmDiskSpace.FormPaint(Sender: TObject);
var
  BarWidth, I : Integer;
  Koof : Single;
begin

  if DirsCount <> 0 then
  begin
    BarWidth := PaintBox.Width div DirsCount;

    Koof := PaintBox.Height / MaxSize;
    with PaintBox.Canvas do
    begin

      for I := 0 to High(Charts) do
      begin
        Brush.Color := Charts[I].Color;
        Rectangle(I*BarWidth, PaintBox.Height - Trunc(Charts[I].Size * Koof), I*BarWidth+BarWidth, PaintBox.Height);
      end;
    end;
  end;
end;

procedure TfrmDiskSpace.FormResize(Sender: TObject);
begin
  CountDir;
  Repaint;
end;

end.
